﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using VoicePhone.Common;

namespace VoicePhone.Controls
{
    public class WindowBase : Window
    {
        public WindowBase()
        {
            this.Loaded += WindowBase_Loaded;
        }

        private void WindowBase_Loaded(object sender, RoutedEventArgs e)
        {
            VisualHelper.FindVisualChild<Label>(this).MouseLeftButtonDown += WindowBase_MouseLeftButtonDown;
        }

        public void WindowBase_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.DragMove();
        }

        public ObservableCollection<DataGridColumn> Columns
        {
            get { return (ObservableCollection<DataGridColumn>)GetValue(ColumnsProperty); }
            set { SetValue(ColumnsProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Columns.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ColumnsProperty =
            DependencyProperty.Register("Columns", typeof(ObservableCollection<DataGridColumn>), typeof(WindowBase), new PropertyMetadata(ColumnsChangedCallback));

        public static void ColumnsChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var win = d as WindowBase;
            Grid gdColumns = VisualHelper.FindVisualChild<Grid>(win, "gdColumns");
            gdColumns.ColumnDefinitions.Clear();
            foreach (DataGridColumn column in win.Columns)
            {
                ColumnDefinition columnDefinition = new ColumnDefinition()
                {
                    Width = new GridLength(column.Width.Value, column.Width.IsStar ? GridUnitType.Star : GridUnitType.Pixel)
                };
                Label label = new Label()
                {
                    Content = column.Header.ToString(),
                    HorizontalContentAlignment = HorizontalAlignment.Center,
                    HorizontalAlignment = HorizontalAlignment.Center,
                    FontSize = 18,
                    Foreground = Brushes.White
                };
                
                gdColumns.ColumnDefinitions.Add(columnDefinition);
                Grid.SetColumn(label, gdColumns.ColumnDefinitions.Count - 1);
                gdColumns.Children.Add(label);
            }
        }
    }
}
